<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .wrap {
            width: 300px;
            margin: 100px auto 0;
        }

        table {
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
            width: 300px;
        }

        th,
        td {
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }

        th {
            background-color: #09c;
            font: bold 16px "微软雅黑";
            color: #fff;
        }

        td {
            font: 14px "微软雅黑";
        }

        tbody tr {
            background-color: #f0f0f0;
        }

        tbody tr:hover {
            cursor: pointer;
            background-color: #fafafa;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <table>
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="zx_cbAll" />
                    </th>
                    <th>机型</th>
                    <th>价钱</th>
                </tr>
            </thead>
            <tbody id="zx_tb">
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>荣耀20</td>
                    <td>2699起</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>Mate30</td>
                    <td>5799起</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>HUAWEI P30</td>
                    <td>4988起</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>华为平板 M6</td>
                    <td>2249起</td>
                </tr>
            </tbody>
        </table>
    </div>

</body>
<script>

// 1.获取元素对象
var all = document.querySelector('#zx_cbAll');
var box = document.querySelector('#zx_tb').querySelectorAll('input');

// 2.给全选按钮绑定点击事件
all.onclick = function(){
    // 循环遍历每个单选点击框
    for(var i = 0; i < box.length; i++){
        // 当点击全选按钮时，让每个单选点击框和全选点击框的状态一致
        box[i].checked = all.checked;
    }
}

// 3.给单选框 添加点击事件
// 循环遍历单选框 
for(var i = 0; i< box.length; i++){
    // 给当前点击的这个单选框 绑定点击事件
    box[i].onclick = function(){
        // 4.单选框只要有一个状态是未选中，则全选框也是不选中
        // 先假设是全部选中的
        var flag = true;
        for(var i = 0; i < box.length; i++){
            if(!box[i].checked){
                flag = false;
                break;//有一个条件满足 就跳出当前循环
            }
        }
        all.checked = flag;
    }
}



</script>

</html>